53 uint32_t ports_available;
54 size_t buffer_available;
75 static bool is_valid_vid_pid(
const char* vid,
const char* pid)
77 if (strlen(vid) != 4 || strlen(pid) != 4)
80 for (
int i = 0; i < 4; i++)
82 if (!isxdigit(vid[i]) || !isxdigit(pid[i]))
104 static bool com_find(
const char* vid,
const char* pid,
105 void(*callback)(
const char* com_port,
void* context),
108 if (!is_valid_vid_pid(vid, pid))
113 snprintf(pnpid,
sizeof(pnpid),
"USB\\VID_%c%c%c%c&PID_%c%c%c%c",
114 toupper(vid[0]), toupper(vid[1]), toupper(vid[2]), toupper(vid[3]),
115 toupper(pid[0]), toupper(pid[1]), toupper(pid[2]), toupper(pid[3]));
119 HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(NULL, TEXT(
"USB"), NULL,
120 DIGCF_ALLCLASSES | DIGCF_PRESENT);
121 if (DeviceInfoSet == INVALID_HANDLE_VALUE)
124 DWORD DeviceIndex = 0;
125 SP_DEVINFO_DATA DeviceInfoData = {0};
126 DeviceInfoData.cbSize =
sizeof(SP_DEVINFO_DATA);
130 while (SetupDiEnumDeviceInfo(DeviceInfoSet, DeviceIndex++, &DeviceInfoData))
132 BYTE buffer[256] = {0};
133 DEVPROPTYPE ulPropertyType;
137 if (SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet, &DeviceInfoData,
138 SPDRP_HARDWAREID, &ulPropertyType,
139 buffer,
sizeof(buffer), &dwSize1))
142 if (strncmp((
char*)buffer, pnpid, strlen(pnpid)) != 0)
146 HKEY hDeviceRegistryKey = SetupDiOpenDevRegKey(DeviceInfoSet,
149 DIREG_DEV, KEY_READ);
150 if (hDeviceRegistryKey == INVALID_HANDLE_VALUE)
154 char port_name[32] = {0};
155 DWORD dwSize2 =
sizeof(port_name);
156 DWORD dwType = REG_SZ;
157 LSTATUS ret = RegQueryValueExA(hDeviceRegistryKey,
"PortName",
158 NULL, &dwType, (LPBYTE)port_name, &dwSize2);
161 RegCloseKey(hDeviceRegistryKey);
163 if (ret == ERROR_SUCCESS && (dwType == REG_SZ))
166 if (strncmp(port_name,
"COM", 3) == 0)
167 callback(port_name, context);
172 SetupDiDestroyDeviceInfoList(DeviceInfoSet);
177 static void callback(
const char* com_port,
void* context)
179 Port_List_t* ports = (Port_List_t*)context;
180 const size_t len = strlen(com_port);
182 if (ports->ports_available == 0 && ports->buffer_available >= len)
184 memcpy(ports->port_list, com_port, len);
185 ports->port_list += len;
186 ports->buffer_available -= len;
188 else if (ports->ports_available > 0 && ports->buffer_available >= (len + 1))
190 *ports->port_list++ =
';';
191 memcpy(ports->port_list, com_port, len);
192 ports->port_list += len;
193 ports->buffer_available -= len + 1;
196 ports->ports_available++;
208 ports.port_list = port_list;
209 ports.buffer_available = buffer_size - 1;
210 ports.ports_available = 0;
212 memset(port_list, 0, buffer_size);
217 return ports.ports_available;
222 COMMTIMEOUTS timeouts;
223 DCB com_configuration = {0};
226 if (com_port == NULL)
238 #define T_BUFFER_LENGTH 32 239 TCHAR full_port_name[T_BUFFER_LENGTH] = TEXT(
"\\\\.\\");
241 while ((port_name[idx] != 0) && (idx + 5 < T_BUFFER_LENGTH))
243 full_port_name[4 + idx] = port_name[idx];
246 full_port_name[4 + idx] = 0;
250 com_port->handle = CreateFile(full_port_name,
262 if (com_port->handle == INVALID_HANDLE_VALUE)
268 timeouts.ReadIntervalTimeout = 0;
269 timeouts.ReadTotalTimeoutMultiplier = 0;
270 timeouts.ReadTotalTimeoutConstant = 1000;
271 timeouts.WriteTotalTimeoutConstant = 100;
272 timeouts.WriteTotalTimeoutMultiplier = 1;
273 (void)SetCommTimeouts(com_port->handle, &timeouts);
276 com_configuration.DCBlength =
sizeof(DCB);
277 (void)GetCommState (com_port->handle, &com_configuration);
278 com_configuration.BaudRate = 115200;
279 com_configuration.ByteSize = 8;
280 com_configuration.StopBits = ONESTOPBIT;
281 (void)SetCommState (com_port->handle, &com_configuration);
288 if (com_port == NULL)
292 (void)CancelIo(com_port->handle);
295 (void)CloseHandle(com_port->handle);
304 DWORD num_bytes_written;
305 WriteFile(com_port->handle, data, (DWORD)num_bytes, &num_bytes_written, NULL);
306 return num_bytes_written;
310 void* data,
size_t num_requested_bytes)
313 DWORD num_bytes_read = 0;
315 (void)ReadFile(com_port->handle, data, (DWORD)num_requested_bytes, &num_bytes_read, NULL);
317 return num_bytes_read;
323 COMMTIMEOUTS timeouts;
324 timeouts.ReadIntervalTimeout = 0;
325 timeouts.ReadTotalTimeoutMultiplier = 0;
326 timeouts.ReadTotalTimeoutConstant = timeout_period_ms;
327 timeouts.WriteTotalTimeoutConstant = 100;
328 timeouts.WriteTotalTimeoutMultiplier = 1;
329 (void)SetCommTimeouts(com_port->handle, &timeouts);
void com_set_timeout(com_t *com_port, uint32_t timeout_period_ms)
This function changes the timeout period of an open COM port.
size_t com_get_data(com_t *com_port, void *data, size_t num_requested_bytes)
This function reads data from an open COM port.
void com_close(com_t *com_port)
This function closes the specified COM port.
This file defines the API to access a serial communication port.
size_t com_send_data(com_t *com_port, const void *data, size_t num_bytes)
This function sends data through an open COM port.
com_t * com_open(const char *port_name)
This functions opens the specified COM port.
uint32_t com_get_port_list(char *port_list, size_t buffer_size)
This function returns a list of available COM ports.